package com.example.service;

import com.example.common.enums.ModuleEnum;
import com.example.controller.WorksController;
import com.example.controller.domain.SearchContent;
import com.example.controller.domain.SearchRequest;
import com.example.controller.domain.UserRecentSearch;
import com.example.entity.Notebook;
import com.example.entity.Works;
import com.example.mapper.NotebookMapper;
import com.example.mapper.WorksMapper;
import com.example.utils.SearchRedisHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Service
public class SearchService {

    private static final Logger log = LoggerFactory.getLogger(SearchService.class);
    @Resource
    private SearchRedisHelper searchRedisHelper;
    @Resource
    private WorksMapper worksMapper;
    @Resource
    private NotebookMapper notebookMapper;

    /**
     * 搜索
     * @param searchRequest
     * @return
     */
    public void search(SearchRequest searchRequest,String module) {
        //业务代码可用es.....此处略过....模拟数据库数据
        List<SearchContent> searchContentList = new ArrayList();
        if(module.equals(ModuleEnum.WORKS.getValue())){
            List<Works> list=worksMapper.queryForListBySearchKeyword(searchRequest);
            list.forEach(work->{
                SearchContent searchContent = new SearchContent();
                searchContent.setFid(work.getId());
                searchContent.setModule(module);
                searchContent.setName(work.getName());
                searchContentList.add(searchContent);
            });
        }else{
            List<Notebook> list=notebookMapper.queryForListBySearchKeyword(searchRequest);
            list.forEach(notebook->{
                SearchContent searchContent = new SearchContent();
                searchContent.setFid(notebook.getId());
                searchContent.setModule(module);
                searchContent.setName(notebook.getTitle());
                searchContentList.add(searchContent);
            });
        }
        searchRedisHelper.addRedisRecentSearch(searchRequest.getSearchKeyword(),module);
        searchRedisHelper.addRedisHotSearchList(searchContentList,1L);
    }

    /**
     * 热搜列表
     * @return
     */
    public Set<SearchContent> listHotSearch() {
        log.info("set=>{}",searchRedisHelper.listHotSearch());
        return searchRedisHelper.listHotSearch();
    }

    /**
     * 最近搜索列表
     * @return
     */
    public Set<UserRecentSearch> listRecentSearch() {
        log.info("set=>{}",searchRedisHelper.listRecentSearch());
        return searchRedisHelper.listRecentSearch();
    }
    public long removeRecentSearch(UserRecentSearch userRecentSearch){
        log.info("userRecentSearch=>{}",userRecentSearch);
        return searchRedisHelper.removeRedisRecentSearch(userRecentSearch);
    }

    public void removeRecentSearchList(){
            searchRedisHelper.removeRedisRecentSearchList();
    }
}
